package zzz.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import zzz.core.common.HttpUtils;
import zzz.model.Advertising;
import zzz.model.Company;
import zzz.model.EsMessage;
import zzz.service.AdverService;
import zzz.service.CompanyService;

@Controller
public class IndexAction {
	@Autowired
	public CompanyService companyService;
	@Autowired
	public AdverService adverService;
	
	public static final String APPKEY = "c69354878276b244";// 你的appkey
    public static final String URL = "http://api.jisuapi.com/express/type";
    public static final String URLMSG="http://api.jisuapi.com/express/query";
    public static List<Company> GetCompanys() {
        String result = null;
        String url = URL + "?appkey=" + APPKEY;
        List<Company> companys=new ArrayList<Company>();
        try {
        	
            result = HttpUtils.get(url);
            System.out.println("result"+result);
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
                JSONArray resultarr = json.optJSONArray("result");
                System.out.println(resultarr.toString());
                
                for (int i = 0; i < resultarr.size(); i++) {
                    JSONObject obj = (JSONObject) resultarr.opt(i);
                    Company c=new Company();
                    String name = obj.getString("name");
                    String type = obj.getString("type");
                    c.setEcName(name);
                    c.setEcType(type);
                    companys.add(c);
                   /* String letter = obj.getString("letter");
                    String tel = obj.getString("tel");
                    String number = obj.getString("number");*/
                    System.out.println(name + "    " + type );
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return companys;
    }
	@RequestMapping("/index")
	public String getIndexPage(Map<String,List> m){
		List<Company> companys=companyService.getAllCompanys();
		List<Company> companyss=GetCompanys();
		List<Advertising> advers=adverService.getAllAdver(); 
		m.put("companys", companys);
		m.put("companyss", companyss);
		m.put("advers", advers);
		System.out.println(companys.toString());
		return "homePage";
		//return "manager/adManager";
	}
	@RequestMapping("/managerindex")
	public String getManageIndexPage(Map<String,List> m){
		return "manager/managerLogin";
	}
	@RequestMapping("/msgResult")
	public String getMsg(Map<String,List> m){
		return "common/msgModal";
	}
	@RequestMapping("/getecmsg")
	public String getEcMsg(String ecNumber,String ecType,Map<String,List> m){
		Map<String,String> param=new HashMap<String,String>();
		List<EsMessage> EsMessages=new ArrayList<EsMessage>();
		param.put("type", ecType);
		param.put("number", ecNumber);
		String result = null;
        String url = URLMSG + "?appkey=" + APPKEY;
        try {
        	
            result = HttpUtils.post(url,param);
            System.out.println("result"+result);
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
            	JSONObject res=json.optJSONObject("result");
                JSONArray resultarr = res.optJSONArray("list");
                System.out.println(resultarr.toString());
                
                for (int i = 0; i < resultarr.size(); i++) {
                    JSONObject obj = (JSONObject) resultarr.opt(i);
                    EsMessage e=new EsMessage();
                    String time = obj.getString("time");
                    String status = obj.getString("status");
                    e.setTime(time);
                    e.setStatus(status);
                    EsMessages.add(e);
                    System.out.println(time + "    " + status );
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        m.put("EsMessage", EsMessages);
		return "common/esMsgPage";
	}
}
